package com.shinlooker.wzyd.activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.shinlooker.wzyd.R;


/**
 * Created by shinlooker@qq.com
 * on 2020/6/7
 */
public class DetailVideoActivity extends AppCompatActivity {


    private ImageView detial_img1;
    private ImageView detial_img2;
    private TextView detail_str1;
    private TextView detail_str2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_video);
        final VideoView videoView = (VideoView) findViewById(R.id.videoView);
        detial_img1 = findViewById(R.id.detial_video_img1);
        detial_img2 = findViewById(R.id.detial_video_img2);
        detail_str1 = findViewById(R.id.detial_video_str1);
        detail_str2 = findViewById(R.id.detial_video_str2);

        int video = getIntent().getIntExtra("video", 0);
        int img1 = getIntent().getIntExtra("img1", 0);
        int img2 = getIntent().getIntExtra("img2", 0);
        int str1 = getIntent().getIntExtra("str1", 0);
        int str2 = getIntent().getIntExtra("str2", 0);
        detial_img1.setImageResource(img1);
        detial_img2.setImageResource(img2);
        detail_str1.setText(str1);
        detail_str2.setText(str2);

        //加载指定的视频文件
        String uri = "android.resource://" + getPackageName() + "/" + video;
        videoView.setVideoPath(uri);
        videoView.start();
        //设置点击事件，OnClickListener不好用
        videoView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (videoView.isPlaying()) {
                    videoView.pause();
                } else {
                    videoView.start();
                }
                return false;
            }
        });

        //设置循环播放
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.start();
                mp.setLooping(true);
            }
        });
    }
}
